<?php
/**
 * Created by xiaokengge
 * User: zhaoyu
 * Date: 2024/1/25
 * Time: 15:20
 *describe:****
 */

namespace app\api\validate;



use app\api\service\ThrowNewErr;
use think\Request;
use think\Validate;

class BaseValidate extends Validate
{
    /**
     * goCheck 校验方法
     * author : Terry
     * date   : 19:57
     * return : bool
     */
    public function goCheck()
    {
        //对这些参数做校验 基础参数验证
        request()->filter(['strip_tags','htmlspecialchars']);//过滤规则
        $param = request()->param();
        $result = $this->batch()->check($param);
        if (!$result) {

//            throw new ParameterException(['msg' => $this->error]);
            $this->error = is_array($this->error) ? implode(',',$this->error) :$this->error;
            (new ThrowNewErr())->validateFailException($this->error);
        } else {
            return true;
        }
    }

    /**
     * isPositveIntteger 校验id是否为正整数
     * @param $value
     * @param string $rule
     * @param string $data
     * @param string $field
     * author : Terry
     * date   : 16:56
     * return : bool
     */
    protected function isPositiveInteger($value, $rule = '', $data = '', $field = '')
    {

        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * isAdmin 判断你是否是超级管理员
     * @param * @param $value
     * url    :
     * method :
     * author : Terry
     * date   : 16:52
     * return : bool
     */
    protected function isAdmin($value)
    {
        if (intval($value) === 1) {
            return false;
        }
        return true;
    }

    /**
     * isNotEmpty 判断参数是否为空
     * author : Terry
     * date   : 07:23
     * return : bool
     */
    protected function isNotEmpty($value, $rule = '', $data = '', $field = '')
    {
        if (empty($value)) {
            return false;
        } else {
            return true;
        }

    }

    /**验证是不是数字
     * @param $value
     * @return bool
     */
    protected function isNumber($value)
    {
        if (is_numeric($value)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * getDataByRule 过滤参数中非法数据
     * @param $arrays 用户参数
     * author : Terry
     * date   : 16:46
     * return : array
     */
    public function getDataByRule($arrays)
    {
        if (array_key_exists('user_id', $arrays) | array_key_exists('uid', $arrays)) {
//            throw new ParameterException([
//                'msg' => '含有非法参数uid或user_id'
//            ]);
            (new ThrowNewErr())->validateFailException('含有非法参数uid或user_id');
        }
        $newArray = [];

        foreach ($this->rule as $key => $value) {
            $newArray[$key] = $arrays[$key];
        }
        return $newArray;
    }

    /**
     * isMobile 校验手机号
     * @param $value
     * author : Terry
     * date   : 16:59
     * return : bool
     */
    public function isMobile($value)
    {
        $rule = '^1(3|6|5|7|8|4|9|6)[0-9]\d{8}$^';
        $result = preg_match($rule, $value);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**验证座机
     * @param $value
     * @return bool
     * Author: zhaoyu
     * Date: 2020/8/13 11:23
     */
    public  function isTel($value){
        $isTel="/^400(-\d{3,4}){2}$/";
        $result = preg_match($isTel, $value);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**检验数组中值不能为空
     * @param $data
     * @return bool
     * Author: zhaoyu
     * Date: 2020/8/10 17:02
     */
    protected  function checkArrayFieldValue($data){
        if(!is_array($data)){
            return false;
        }
        foreach ($data as $k=>$v){
            if(empty($v)){
                return false;
            }
        }
        return true;
    }



}
